package com.ny.dao.lmpl;

import com.mysql.cj.protocol.Resultset;
import com.ny.dao.ProductDao;
import com.ny.entity.Product;
import com.ny.until.SqlConncetion;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao {
    Connection connection = SqlConncetion.getConnection();

    public ProductDaoImpl() throws SQLException, ClassNotFoundException {
    }



    @Override
    public List<Product> findAllProduct() throws SQLException {
        String sql = "select * from products";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        List<Product> productList = new ArrayList<>();
        while (resultSet.next()) {
            Product product = new Product();
            product.setProduct_Id(resultSet.getInt("product_id"));
            product.setName(resultSet.getString("name"));
            product.setPrice(resultSet.getDouble("price"));
            product.setStock(resultSet.getInt("stock"));
            product.setCategory(resultSet.getString("category"));
            product.setDescription(resultSet.getString("description"));
            productList.add(product);
        }
        return productList;
    }


    @Override
    public Product deleteProduct(String name) throws SQLException {
        String sql = "delete from products where name = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, name);
        int i = preparedStatement.executeUpdate();
        if (i > 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    return null;}
}